//
//  ONUtility.m
//  OrderNow
//
//  Created by Chickennohair on 2014/03/15.
//  Copyright (c) 2014年 Familly. All rights reserved.
//

#import "ONUtility.h"
#import "ONFetcher.h"

@implementation ONUtility
+(float)degreeToRadian:(float) x {
    return x * M_PI / 180;
}

+(CGFloat) calculateDistanceBetweenLocation:(CLLocationCoordinate2D )p1 withLocation:(CLLocationCoordinate2D )p2 {
    CGFloat R = 6378.137; // Earth’s mean radius in meter
    CGFloat dLat = [ONUtility degreeToRadian:(p2.latitude - p1.latitude)];//rad(p2.lat() - p1.lat());
    CGFloat dLong = [ONUtility degreeToRadian:p2.longitude - p1.longitude];//rad(p2.lng() - p1.lng());
    CGFloat a = sinf(dLat / 2) * sinf(dLat / 2) +
    cosf([ONUtility degreeToRadian:(p1.latitude)]) * cosf([ONUtility degreeToRadian:p2.latitude]) *
    sinf(dLong / 2) * sinf(dLong / 2);
    CGFloat c = 2 * atan2(sqrtf(a), sqrtf(1 - a));
    CGFloat d = R * c;
    return d;//d; // returns the distance in meter
};




+(BOOL)validateName:(NSString *)name{
    NSString *myRegex = @"[A-Z0-9a-z ]*";
    NSPredicate *myTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", myRegex];
    return [myTest evaluateWithObject:name];
}


+(NSString*)formatMoney:(CGFloat) money{
    NSString *numberString = [NSString stringWithFormat:@"%.2f",money];
    NSString *currentLanguage = [NSLocale preferredLanguages][0];
    if ([currentLanguage isEqualToString:@"nl"]) {
        numberString = [numberString stringByReplacingOccurrencesOfString:@"." withString:@","];
    }
    return numberString;
//    
//    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
//    [numberFormatter setGroupingSeparator:@","];
//    [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle];
//    return [numberFormatter stringFromNumber:[NSNumber numberWithFloat:money]];
}
+(void)handleErrorWithResponse:(NSData *)response{
    id object = [response objectFromJSONData];
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSString *errorCode = @"";
        if ([object objectForKey:@"errorCode"]) {
            errorCode = [object objectForKey:@"errorCode"];
        }
        if ([object objectForKey:@"code"]) {
            errorCode = [object objectForKey:@"code"];
        }
        NSString *errorMessage = @"";
        switch ([errorCode integerValue]) {
            case 1001:
                errorMessage = @"That location could not be found.";
                break;
            case 1002:
                errorMessage = @"The take-out time you selected is not accepted, please choose another take-out time.";
                break;
            case 1003:
                errorMessage = @"The table code you scanned is invalid or not recognized.";
                break;
            case 1004:
                errorMessage = @"Your name is missing.";
                break;
            case 1005:
                errorMessage = @"Your phone number is missing.";
                break;
            case 1006:
                errorMessage = @"Your e-mail address is missing.";
                break;
            case 1007:
                errorMessage = @"The order you are trying to place is invalid.";
                break;
            case 1008:
                errorMessage = @"The payment could not be processed.";
                break;
            case 1009:
                errorMessage = @"That order could not be found.";
                break;
            case 1010:
                errorMessage = @"The order can't be cancelled due to the maximum cancellation period.";
                break;
            case 1011:
                errorMessage = @"The sending method that was chosen is not valid.";
                break;
            case 1012:
                errorMessage = @"One or more sending destinations is invalid.";
                break;
            default:
                break;
        }
        
        if ([errorMessage isEqualToString:@""] == YES) {
            errorMessage = @"Failed!";
            return;
        }
        @autoreleasepool {
//            ONAlertView * alertView=[[ONAlertView alloc] initWithFrame:[[UIScreen mainScreen] bounds] andText:errorMessage];
//            [APP_DELEGATE.window addSubview:alertView];
            
        }
    }
}


@end
